package school30.io.fs;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import school30.io.File;
import school30.io.FileConnector;
import school30.io.FileReader;
import school30.io.FileWriter;

public class FileSystemConnector extends FileConnector {

    public Vector roots = new Vector();

    public FileSystemConnector() {
        Enumeration e = FileSystemRegistry.listRoots();
        while (e.hasMoreElements()) {
            roots.addElement(e.nextElement());
        }
        for (int i = 0; i < roots.size(); i++)
        {
            System.out.println(roots.elementAt(i));
        }
    }

    public FileReader openRead(File f) {
        try {
            FileConnection fc = (FileConnection) Connector.open("file://" + f.path(),
                    Connector.READ);
            return new FsReader(fc);
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }

    }

    public FileWriter openWrite(File f) {
        try {
            FileConnection fc = (FileConnection) Connector.open("file://" + f.path(),
                    Connector.WRITE);
            return new FsWriter(fc);
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public long size(File f) {
        try {
            FileConnection fc = (FileConnection) Connector.open("file://" + f.path(),
                    Connector.READ);
            if (!fc.isDirectory()) {
                long size = fc.fileSize();
                fc.close();
                return size;
            } else {
                fc.close();
                return -2;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public void deleteFile(File f) {
        try {
            FileConnection fc = (FileConnection) Connector.open("file://" + f.path());
            fc.delete();
            fc.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void createFolder(File f) {
        try {
            FileConnection fc = (FileConnection) Connector.open("file://" + f.path());
            fc.mkdir();
            fc.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public boolean isFolder(File f) {
        try {
            FileConnection fc = (FileConnection) Connector.open("file://" + f.path());
            boolean id = fc.isDirectory();
            fc.close();
            return id;
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public String[] folderContents(File f) {
        if (f.isRoot())
        {
            String[] rts = new String[roots.size()];
            for (int i = 0; i < rts.length; i++)
            {
                rts[i] = (String) roots.elementAt(i);
            }
            return rts;
        }
        try {
            FileConnection fc = (FileConnection) Connector.open("file://" + f.path());
            Enumeration e = fc.list();
            Vector v = new Vector();
            while (e.hasMoreElements()) {
                v.addElement(e.nextElement());
            }
            String[] k = new String[v.size()];
            for (int i = 0; i < v.size(); i++) {
                k[i] = (String) v.elementAt(i);
            }
            fc.close();
            return k;
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
